myApp.controller('addEmployee', ['$scope', '$http', function($scope, $http){

	$scope.userName = "";
	$scope.password = "";
	$scope.name = "";
	$scope.phone = "";
	$scope.positionId = "";
	$scope.allPosition = [];
	$scope.salary = "";
	$scope.hiredate = "";
	$scope.shopId = "";
	$scope.allShop = [];

	$scope.goodsIM = false;
	$scope.saleIM = false;
	$scope.purchaseIM = false;
	$scope.shopIM = false;
	$scope.stockIM = false;
	$scope.customerIM = false;
	$scope.financeIM = false;
	$scope.decisionIM = false;
	$scope.adminIM = false;
	var jurisdictionId = "";

	(function() {
		$http.get('searchAllPosition').then(function successCallback(response) {
			
			$scope.allPosition = response.data;

		}, function errorCallback(response) {
			console.log("fail：" + response);
		});		
	})();

	(function() {
		$http.get('searchAllShop').then(function successCallback(response) {
			
			$scope.allShop = response.data;

		}, function errorCallback(response) {
			console.log("fail：" + response);
		});		
	})();

	$scope.addEmployee = function() {

		if($scope.goodsIM === true) {
			jurisdictionId += "1";
		}
		if($scope.saleIM === true) {
			jurisdictionId += ",2";
		}
		if($scope.purchaseIM === true) {
			jurisdictionId += ",3";
		}
		if($scope.shopIM === true) {
			jurisdictionId += ",4";
		}
		if($scope.stockIM === true) {
			jurisdictionId += ",5";
		}
		if($scope.customerIM === true) {
			jurisdictionId += ",6";
		}
		if($scope.financeIM === true) {
			jurisdictionId += ",7";
		}
		if($scope.decisionIM === true) {
			jurisdictionId += ",8";
		}
		if($scope.adminIM === true) {
			jurisdictionId += ",9";
		}

		if($scope.userName === "" || $scope.password === "" || $scope.name === "" || 
			$scope.phone === "" || $scope.positionId === "" || $scope.salary === "" ||
			$scope.hiredate === "" || $scope.shopId === "" || $scope.jurisdictionId === "") {
			alert("请完善信息！");
		return;
	}


	var hiredate = new Date($scope.hiredate);
	var year = hiredate.getFullYear() - 1900;
	var month = hiredate.getMonth();
	var date = hiredate.getDate();

	var employeeData = {
		"employee.userName": $scope.userName,
		"employee.password": $scope.password,
		"employee.shopId": $scope.shopId,
		"employee.name": $scope.name,
		"employee.phone": $scope.phone,
		"employee.positionId": $scope.positionId,
		"employee.salary": $scope.salary,
		"year": year,
		"month": month,
		"date": date,
		"employee.jurisdictionId": jurisdictionId,

	};

	var postCfg = {
		headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
		transformRequest: function (data) {
			return $.param(data);
		}
	};

	$http.post('addEmployee', employeeData, postCfg).then(function successCallback(response) {
		alert("添加成功！");

		$scope.userName = "";
		$scope.password = "";
		$scope.name = "";
		$scope.phone = "";
		$scope.positionId = "";
		$scope.salary = "";
		$scope.hiredate = "";
		$scope.shopId = "";

		$scope.goodsIM = false;
		$scope.saleIM = false;
		$scope.purchaseIM = false;
		$scope.shopIM = false;
		$scope.stockIM = false;
		$scope.customerIM = false;
		$scope.financeIM = false;
		$scope.decisionIM = false;
		$scope.adminIM = false;
		jurisdictionId = "";

		console.log("ok：" + response);
	}, function errorCallback(response) {
		alert("查询操作失败!");
		console.log("fail：" + response);
	});

};

}]);